<?

require_once("lightconnexceptions.class.php");
require_once("lightconniterator.class.php");
require_once("drivers/driver.interface.class.php");

class LightConnection
{
	/**
	 * Objeto driver do LighConn
	 *
	 * @var LightConnDriver
	 */
	protected $driver;
	
	/**
	* @var Context
	*/
	protected $_context = null;	
	
	protected $_alias = 'default';
	
	protected $host;
	protected $user;
	protected $password ;
	protected $database ;
	protected $persistente ;
	
	/**
	* Base Class Constructor. Don't must be override. The Context Class is required.
	* 
	* @param Context $context
	*/
	function __construct()
	{		
		$this->_context = $context = Context::getInstance();//Context::getInstance();
		
		$db = $this->_context->getDbConfig("DATABASE");
		$db = $db[$this->_alias];
		//DebugUtil::PrintValue($db,"line: ".__LINE__ );exit;
		$this->host = $db["host"];
		$this->user = $db["login"];
		$this->password = $db["password"];
		$this->database = $db["database"];
		$this->persistente = $db["persistente"];
		$this->type = $db["driver"];
		
		$this->getDBDataSet();
	}	
	
	
	/**
	 * Create a instance of DBDataSet to connect database
	 *
	 */
	function getDBDataSet()
	{
		self::LoadDriver($this->type);
		
		switch (strtolower($this->type)) {
			case 'mysql': $this->driver = new LightConn_mysql(); $this->driver->Setup(); break;
		
			default: throw new LightConnException("Driver não encontrado"); break;
		}
		
		
		$this->Connect();
	}
	
	/**
	 * Carrega uma biblioteca driver de um banco
	 *
	 * @param string $type
	 */
	public static function LoadDriver($type)
	{
		static $arrLoadedDrivers;
		
		if (!$arrLoadedDrivers[$type])
		{
			$path = dirname(__FILE__) . '/';
			if (!file_exists($path . "drivers/driver." . $type . ".class.php"))
			{
				throw new LightConnException("Driver $type inválido");
			}
			require_once("drivers/driver." . $type . ".class.php");
			$arrLoadedDrivers[$type] = true;
		}
	}	
	
	protected function Connect()
	{
		$this->driver->Connect($this->host, $this->user, $this->password, $this->database,$this->persistente);
	}
	
	/**
	 * Executa um query SQL
	 *
	 * @param string $sql
	 * @param array $prepare
	 * @return LightConnIterator
	 */
	public function Query($sql, $prepare = null, $oneLine = false)
	{
		return $this->driver->Query($sql, $prepare , $oneLine);
	}
	
	/**
	 * Executa um query SQL e retorna o número de registros afetados
	 *
	 * @param string $sql
	 * @param array $prepare
	 * @return int
	 */
	public function Execute($sql, $prepare = null)
	{
		return $this->driver->Execute($sql, $prepare);
	}
	
	/**
	 * Fechar a conexão com o banco
	 *
	 */
	public function Disconnect()
	{
		$this->driver->Disconnect();
	}
	
	/**
	 * Importar SQL
	 *
	 * @param string $sql
	 * @return bool
	 */
	public function ImportSQL($sql)
	{
		$this->driver->ImportSQL($sql);
	}
	
	/**
	 * Importar um arquivo SQL
	 *
	 * @param string $file
	 * @return bool
	 */
	public function Import($file)
	{
		$this->driver->Import($file);
	}
	
	/**
	 * Concluir transação
	 *
	 * @return bool
	 */
	public function Commit()
	{
		return $this->driver->Commit();
	}
	
	/**
	 * Cancelar transação
	 *
	 * @return bool
	 */
	public function Rollback()
	{
		return $this->driver->Rollback();
	}
	
	/**
	 * Retorna ultimo ID
	 * @return LightConnIterator
	 */
	public function LasId()
	{	
		return $this->driver->LastInsertId();
	}
}